import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Product, ProductService, Comment } from '../shared/product.service';

@Component({
  selector: 'app-product-detail',
  templateUrl: './product-detail.component.html',
  styleUrls: ['./product-detail.component.css']
})
export class ProductDetailComponent implements OnInit {

  product : Product;
  comments:Comment[];

  //最新评价
  newRating:number=5;
  newComment:string="";
  
  constructor(private routeInfo:ActivatedRoute,private productService:ProductService) { }

  ngOnInit() {
    let id = this.routeInfo.snapshot.params['id'];
    this.product=this.productService.getProduct(id);
    this.comments=this.productService.getComments(id);
  }

  addComment(){
    let comment=new Comment(0,this.product.id,new Date().toLocaleString(),"some one",this.newRating,this.newComment);
    this.comments.unshift(comment);
  }

  getNewRating(rating){
    this.newRating=rating;
  }

}
